package com.qf.shop.system.modules.catgory.controller;

import java.text.ParseException;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.qf.shop.common.core.utils.R;
import com.qf.shop.system.commnons.SysConfigConstans;
import com.qf.shop.system.commnons.SystemRedisKey;
import com.qf.shop.system.modules.sys.service.SysConfigService;
import com.qf.shop.system.utils.DateUtils;
import com.qf.shop.system.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qf.shop.system.modules.catgory.entity.CatgoryEntity;
import com.qf.shop.system.modules.catgory.service.CatgoryService;



/**
 * 
 *
 * @author toString
 * @email toString@qq.com
 * @date 2022-05-16 14:18:10
 */
@RestController
@RequestMapping("/catgory")
public class CatgoryController {
    @Autowired
    private CatgoryService catgoryService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 列表
     * @return
     */

    //根据参数获取商品类型总数
    @RequestMapping("/homeList")
    public List<CatgoryEntity> limitList(){
        System.out.println(redisTemplate.getValueSerializer());
        System.out.println(redisTemplate.getHashValueSerializer());
        String key = SystemRedisKey.SHOP_SYSTEM_CATGORY_HOME_KEY;
        System.out.println(key);
        //获取redis
        List<CatgoryEntity> redis = (List<CatgoryEntity>) redisTemplate.opsForValue().get(key);
        //是否存在redis
        if (redis!=null){
            System.out.println("输出的是redis_homelist");
            return redis;
        }

        //获取key
        String value = sysConfigService.getValue(SysConfigConstans.Home_CATGORY_LIMIT);
        //获取组的数量
        int val = Integer.parseInt(value);
        //获取总的数量
        List<CatgoryEntity> list = catgoryService.gethomeList(val);

        //获取系统时间
        long systemtime = System.currentTimeMillis();
        System.out.println(systemtime);
        //设置redis,过期时间到23：59：59
        try {
            redisTemplate.opsForValue().set(key,list,DateUtils.getLastCurrentTime(),TimeUnit.MILLISECONDS);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("输出的是数据库_homelist");
        return list;
    }

    @RequestMapping("/treeList")
    public List<CatgoryEntity> treeLIst(){
        List<CatgoryEntity> list = catgoryService.treeList();
        return list;
    }

    @RequestMapping("/sortList")
    public List treeList() throws ParseException {
        List result;
        List<CatgoryEntity> list1 = new ArrayList<CatgoryEntity>();
        String key = SystemRedisKey.SHOP_SYSTEM_CATGORY_TREELIST_KEY;
        List<CatgoryEntity> redis = (List<CatgoryEntity>) redisTemplate.opsForValue().get(key);
        if (redis != null) {
            System.out.println("输出sort redis");
            result = redis;
        } else {//全部
            List<CatgoryEntity> list = catgoryService.gethomeList(4);//父類
            ArrayList<HashMap<CatgoryEntity, HashMap<String, List<CatgoryEntity>>>> list3 = new ArrayList<>();//子類
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getPid().equals(-1)) {
                    list1.add(list.get(i));
                }
            }
            for (int i = 0; i < list1.size(); i++) {
                HashMap<String, List<CatgoryEntity>> map = new HashMap<>();
                ArrayList<CatgoryEntity> list2 = new ArrayList<CatgoryEntity>();
                Long c = 0l;


                for (int j = 0; j < list.size(); j++) {
                    Long a = list1.get(i).getId();
                    Long b = Long.valueOf(list.get(j).getPid());
                    if (a == b) {
                        c = a;
                        list2.add(list.get(j));
                    }
                }


                for (int j = 0; j < list1.size(); j++) {
                    if (list1.get(j).getId() == c) {
                        list1.get(j).setList(list2);
                    }
                }
                System.out.println("输出sort数据库");
                redisTemplate.opsForValue().set(key, list1, DateUtils.getLastCurrentTime(), TimeUnit.MILLISECONDS);


            }
            result = list1;
        }


        return result;
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = catgoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CatgoryEntity catgory = catgoryService.getById(id);

        return R.ok().put("catgory", catgory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CatgoryEntity catgory){
		catgoryService.save(catgory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CatgoryEntity catgory){
		catgoryService.updateById(catgory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		catgoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
